package com.example.music08.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.music08.R;
import com.example.music08.entity.Music;

import java.text.SimpleDateFormat;
import java.util.List;

public class MusicAdapter extends BaseAdapter {
    List<Music.DataBean> list;

    public MusicAdapter(List<Music.DataBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music,parent,false);
        TextView tvName = convertView.findViewById(R.id.item_name);
        TextView tvDuration = convertView.findViewById(R.id.item_duration);
        ImageView iv = convertView.findViewById(R.id.item_iv);
        tvName.setText(list.get(position).getName());
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        tvDuration.setText(format.format(list.get(position).getDuration()));
        Glide.with(parent.getContext()).load(list.get(position).getPicUrl()).into(iv);
        return convertView;
    }
}
